﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DictionaryDataConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            LTQNLinQDataContext db = new LTQNLinQDataContext();
            //LayNodeSauNodeRoot(db);
            //layNodeConLai(db);
        }

        private static void layNodeConLai(LTQNLinQDataContext db)
        {
            Dictionary dic;

            foreach (var item in db.Lists)
            {

                if (item.Listtype_Id != null && item.Parent != 0)
                {
                    dic = new Dictionary();

                    dic.IndexName = item.Tenchimuc;

                    //         string tenChiMucCha = db.Lists.Single(l => l.List_Id == item.Parent).Tenchimuc;
                    //        int parentID = db.Dictionaries.Single(d => d.IndexName == tenChiMucCha).ID;
                    try
                    {
                        List chiMucCha = db.Lists.Single(l => l.List_Id == item.Parent);
                        string fullNameOld = findNodeFullNameOldTable(chiMucCha);
                        //      List chiMucCha = db.Lists.Single(l => l.List_Id == 355);
                        //IQueryable<Dictionary> parentID = db.Dictionaries.Select(d => d.IndexName == tenChiMucCha,Dictionary);

                        IQueryable<Dictionary> dicRel = from d in db.Dictionaries
                                                        where d.IndexName == chiMucCha.Tenchimuc
                                                        select d;

                        foreach (Dictionary d in dicRel)
                        {
                            string fullNameNew = findNodeFullNameNewTable(d);
                            if (fullNameNew == fullNameOld)
                            {
                                dic.ParentID = d.ID;
                            }
                        }
                        db.Dictionaries.InsertOnSubmit(dic);
                        db.SubmitChanges();
                    }
                    catch (InvalidOperationException e)
                    {
                        if (e.Message != "Sequence contains no elements")
                        {
                            throw;
                        }
                    }
                }
            }
        }

        private static string findNodeFullNameOldTable(List list)
        {
            LTQNLinQDataContext db = new LTQNLinQDataContext();
            int nextParentID = (int)list.Parent;
            string str = list.Tenchimuc;
            while (nextParentID != 0)
            {
                //var node = from l in db.Lists
                //           where l.List_Id == list.Parent
                //           select l.Tenchimuc;
                var node = db.Lists.Single(l => l.List_Id == nextParentID);
                str += node.Tenchimuc;
                nextParentID = (int)node.Parent;
            }
            return str;

        }

        private static string findNodeFullNameNewTable(Dictionary dic)
        {
            LTQNLinQDataContext db = new LTQNLinQDataContext();
            int nextParentID = (int)dic.ParentID;
            string str = dic.IndexName;
            if (nextParentID<=15)
            {
                return str;
            }
            while (nextParentID != 0)
            {
                var node = db.Dictionaries.Single(d => d.ID == nextParentID);
                str += node.IndexName;
                nextParentID = (int)node.ParentID;
                if (nextParentID <= 15)
                {
                    break;
                }
            }
            return str;

        }
        private static void LayNodeSauNodeRoot(LTQNLinQDataContext db)
        {
            Dictionary dic = new Dictionary();

            // lay nhung node sau node root
            foreach (var item in db.Lists)
            {
                try
                {
                    if (item.Listtype_Id != null && item.Parent == 0)
                    {
                        dic = new Dictionary();
                        dic.IndexName = item.Tenchimuc;

                        string listTypeName = db.Listtypes.Single(t => t.Listtype_Id == item.Listtype_Id).Listname;
                        int dicID = db.Dictionaries.Single(d => d.IndexName == listTypeName).ID;
                        dic.ParentID = dicID;
                        db.Dictionaries.InsertOnSubmit(dic);
                        db.SubmitChanges();

                    }
                }
                catch (Exception e)
                {
                    throw;
                }
            }
        }
    }
}
